package org.example.springboot.service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import jakarta.transaction.Transactional;
import org.example.springboot.dao.ProductsMapper;
import org.example.springboot.model.Products;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service

public class ProductsService {
    @Autowired
    private ProductsMapper productsMapper;

    public String insertProduct(String productImage, String productName, String serialNumber, String modelNumber, String serviceCode, String supplierName, String category,String purchasePrice, String salePrice, String warrantyPeriod, String status, String stock, String inOrderId, String outOrderId, String customerName, String projectName,String salesTeam, String afterSalesTeam, String notes){
        productsMapper.insertProduct(productImage,productName,serialNumber,modelNumber,serviceCode,supplierName,category,purchasePrice,salePrice,warrantyPeriod,status,stock,inOrderId,outOrderId,customerName,projectName,salesTeam,afterSalesTeam,notes);
        return "succeed";
    }

    public List<Products> getAllProduct(){
        return productsMapper.getAllProduct();
    }

    public List<Products> findProductByKeyword(String keyword){
        return productsMapper.findProductByKeyword(keyword);
    }

    public Products getProductById(int id){
       return productsMapper.getProductById(id);
    }

    public void deleteProductById(int id){
        productsMapper.deleteProductById(id);
    }

    public void updateProductStock(String stock,int id){
        productsMapper.updateProductStock(stock,id);
    }

    @Transactional
    public void updateProduct(int id, Products products) {
        // 创建 LambdaUpdateWrapper
        UpdateWrapper<Products> updateWrapper = new UpdateWrapper<>();

        // 设置更新条件：WHERE id = #{id}
        updateWrapper.eq("id", id);

        // 动态设置更新字段（仅更新非空字段）
        if (products.getProductName() != null) {
            updateWrapper.set("product_name", products.getProductName());
        }
        if (products.getModelNumber() != null) {
            updateWrapper.set("model_number", products.getModelNumber());
        }
        if (products.getSerialNumber() != null) {
            updateWrapper.set("serial_number", products.getSerialNumber());
        }
        if (products.getServiceCode() != null) {
            updateWrapper.set("service_code", products.getServiceCode());
        }
        if (products.getSupplierName() != null) {
            updateWrapper.set("supplier_name", products.getSupplierName());
        }
        if (products.getCategory() != null) {
            updateWrapper.set("category", products.getCategory());
        }
        if (products.getPurchasePrice() != null) {
            updateWrapper.set("purchase_price", products.getPurchasePrice());
        }
        if (products.getSalePrice() != null) {
            updateWrapper.set("sale_price", products.getSalePrice());
        }
        if (products.getWarrantyPeriod() != null) {
            updateWrapper.set("warranty_period", products.getWarrantyPeriod());
        }
        if (products.getStatus() != null) {
            updateWrapper.set("status", products.getStatus());
        }
        if (products.getStock() != null) {
            updateWrapper.set("stock", products.getStock());
        }
        if (products.getInOrderId() != null) {
            updateWrapper.set("in_order_id", products.getInOrderId());
        }
        if (products.getOutOrderId() != null) {
            updateWrapper.set("out_order_id", products.getOutOrderId());
        }
        if (products.getOutTime() != null) {
            updateWrapper.set("out_time", products.getOutTime());
        }
        if (products.getCustomerName() != null) {
            updateWrapper.set("customer_name", products.getCustomerName());
        }
        if (products.getProjectName() != null) {
            updateWrapper.set("project_name", products.getProjectName()); // 确保字段名正确
        }
        if (products.getSalesTeam() != null) {
            updateWrapper.set("sales_team", products.getSalesTeam());
        }
        if (products.getAfterSalesTeam() != null) {
            updateWrapper.set("after_sales_team", products.getAfterSalesTeam());
        }
        if (products.getNotes() != null) {
            updateWrapper.set("notes", products.getNotes());
        }
        if (products.getProductImage() != null) {
            updateWrapper.set("product_image", products.getProductImage());
        }
        if (products.getInTime() != null) {
            updateWrapper.set("in_time", products.getInTime());
        }

        // 强制更新必填字段（如 update_time）
        updateWrapper.set("update_time", LocalDateTime.now());
        productsMapper.update(null, updateWrapper);
    }
}
